package com.tg.paw.ums.controller;

import com.alibaba.fastjson.JSON;
import com.tg.paw.common.util.CommonResult;
import com.tg.paw.ums.model.WxJsCode2SessionResult;
import com.tg.paw.ums.service.IWxLoginService;
import com.tg.paw.common.util.TokenData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 微信登录
 * @author Rubble
 * @date 2020/6/24
 */
@Slf4j
@RestController
public class WxStoreLoginController {

    @Autowired
    private IWxLoginService wxLoginService;

    @GetMapping("/notAuth/wx/store/login")
    public CommonResult wxLogin(String code){

        WxJsCode2SessionResult jsCode2SessionResult = wxLoginService.jsCode2SessionResult(code);
        if(jsCode2SessionResult.getErrcode()!=0){
            return CommonResult.failed(JSON.toJSONString(jsCode2SessionResult));
        }

        TokenData tokenData = wxLoginService.wxLogin(jsCode2SessionResult.getOpenid());



        return CommonResult.success(tokenData);
    }
}
